package web.ajax02;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: jqbxd
 * Date: 2009-4-8
 * Time: 14:59:30
 * To change this template use File | Settings | File Templates.
 */
public class GetStockServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HashMap<String,Stock> stocks;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        //返回股票的价格信息
        //1.计算随机数
        double sz = Math.random() * 20;
        double pf = Math.random() * 0.5;
        //通过奇偶数来控制股票的涨跌
        boolean flagz = ((int) (Math.random() * 10)) % 2 == 0;
        boolean flagf = ((int) (Math.random() * 10)) % 2 == 0;
        //2.将随机数和股票的当前价格进行加或者减的操作，得到新的当前价格
        Stock szzs = stocks.get("300001");
        Stock pfyh = stocks.get("000001");
        double temp;
        if (flagz) {
            temp = szzs.getNow() + sz;
        } else {
            temp = szzs.getNow() - sz;
        }
        //需要对新的当前价格进行截取，只保留小数点后两位
        temp = (int) (temp * 100) / 100.0;
        szzs.setNow(temp);
        if (flagf) {
            temp = pfyh.getNow() + pf;
        } else {
            temp = pfyh.getNow() - pf;
        }
        temp = (int) (temp * 100) / 100.0;
        pfyh.setNow(temp);

        PrintWriter out = response.getWriter();
//        out.println(szzs + "<br>" + pfyh);

        //3.返回股票的的昨天收盘价，今天开盘和当前价格
        //采用json的数据格式返回股票信息
        StringBuilder builder = new StringBuilder();
        //采用数组方式回传两个股票对象
//        builder.append("[{name:\"").append(szzs.getName()).append("\",id:\"").append(szzs.getId())
//                .append("\",yes:").append(szzs.getYesterday()).append(",tod:").append(szzs.getToday())
//                .append(",now:").append(szzs.getNow()).append("},")
//                .append("{name:\"").append(pfyh.getName()).append("\",id:\"").append(pfyh.getId())
//                .append("\",yes:").append(pfyh.getYesterday()).append(",tod:").append(pfyh.getToday())
//                .append(",now:").append(pfyh.getNow())
//                .append("}]");
        //采用对象的方式回传两个股票对象       Map： 键值+健名
        //如果回传表示对象的json，需要在外层加上一个括号，否则页面解析会出错
        builder.append("({")
                .append("\"").append(szzs.getId()).append("\":").append("{name:\"").append(szzs.getName())
                .append("\",yes:").append(szzs.getYesterday()).append(",tod:").append(szzs.getToday())
                .append(",now:").append(szzs.getNow()).append("},")
                .append("\"").append(pfyh.getId()).append("\":").append("{name:\"").append(pfyh.getName())
                .append("\",yes:").append(pfyh.getYesterday()).append(",tod:").append(pfyh.getToday())
                .append(",now:").append(pfyh.getNow()).append("}")
                .append("})");
        out.println(builder);
        //json格式数组
//        [{
//            name:"szzs.getName()",
//            id:"szzs.getId()",
//            yes:szzs.getYesterday(),
//            tod:szzs.getToday(),
//            now:szzs.getNow()},
//        {
//            name:"pfyh.getName()",
//            id:"pfyh.getId()",
//            yes:pfyh.getYesterday(),
//            tod:pfyh.getToday(),
//            now:pfyh.getNow()}
//        ]
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        stocks = new HashMap<String, Stock>();
        Stock szzs = new Stock(3000.0, 2009.4, "上证指数", "300001");
        Stock pfyh = new Stock(23.22, 23.22, "浦发银行", "000001");
        //将股票保存在map中
        stocks.put(szzs.getId(), szzs);
        stocks.put(pfyh.getId(), pfyh);
        System.out.println(stocks);
    }
}





























